Script FirstScript

PrintLn(QS_Version());
PrintLn("Aiko.qs 1.0.2");

//1.0.0 	CW	2022/05/26		infinite loop checks is at 1 hour
//1.0.1 	CW	2022/10/02		af_silence

PrintLn(Parameter);

Object o;
Reference p;
U4 ret;
String StrError;

String s;
U0 i;
U4 ConsoleOutFlag = 0;		//default to disable console

switch (GetCount(Parameter))
{
case 4
	ConsoleOutFlag = Parameter[3];
	//fall through
	
case 3
	s = Sys_CurrentDir();
	i = StrFindLast(s, Sys_Constant("DirectorySeparator"));
	if (i != -1)
	{
		StrReplace(s, 0, i + 1, "");			//leave only the currrent folder name
		if (s == Parameter[2])
		{
			PrintLn("Starting ", s);
			if (AddProjectFile("eap.qp") && af_silence("xtap"))
			{
				PrintLn("Unable to load script");
				return;
			}
			
			if (ScriptExist("EntryScript"))
			{
				EntryScript();
			}
		}
	}
	
	if (ConsoleOutFlag == 0)
	{
		Sys_OutputStdout(0);			//disable all the output to Stdout (that means nohup file will not have Print from EAP)
	}

	break;
}

StdIn();
EndScript

Script StdIn
String s;
Vector v;
Reference p;
Boolean t;
PU0 eid;
PU4 status;
PString name;
do
{
	//Print("qs>");
	if (QS_ReadLnStdIn(s))
	{
		PrintLn("No more input from StdIn");
		
		//make sure there are no other active task
		
		WaitIdle:
		VectorClear(eid);
		QS_ListActiveScript(eid, status, name);
		PrintLn(eid);
		if (GetCount(eid) > 1)
		{
			PrintLn("Not all scripts has ended.  Waiting for 1 hour before checking again");
			Sleep(3600000);
			goto WaitIdle;
		}
		return;
	}
	
	t = QS_ParseCommand(s, v);
	if (t && GetCount(v))
	{
		if (s == "q")
		{
			return;
		}
		
		s = v[0];
		VectorRemove(v, 0);
		try
		{
			if (PCallEx(s, v) == 0)
			{
				PrintLn(s, " - script not found");
			}
		}
		catch (p)
		{
			if (RefIsNull(p))
			{
				PrintLn("System exception caught and resume");
				
			}
			else
			{
				PrintLn(p);
				PrintLn("Script exception caught and resume");
			}
		}
	}
} (t);
EndScript

Script sys
U0 loop = 0;
String s;
U0 n = GetCount(Parameter);
while (loop < n)
{
	StrAppend(s, Parameter[loop], " ");
	PrintLn(s);
	Inc(loop);
}

Sys_System(s);

EndScript


Script a
PU0 eid;
PU4 status;
U0 i;
Reference p;
PString name, description;


QS_ListActiveScript(eid, status, name, description);

foreach(p, eid, i)
{
	PrintLn(p, ", ", status[i], ", ", name[i], ", ", description[i]);
}
EndScript


Script k
(x)
QS_KillActiveScript(x);
EndScript

Script l
Reference p;
PString ps;
QS_ListScript(ps);
foreach(p, ps)
{
	PrintLn(p);
}
EndScript

Script r
Reference p;
PString ps;
QS_ListResource(ps);
foreach(p, ps)
{
	PrintLn(p);
}
EndScript

Script s
(x)
String s;
if (GetScript(x, s, 0))
{
	PrintLn(s);
}
else
{
	PrintLn(x, " - script not found");
}
EndScript

Script sd
(x)
String s;
if (GetScript(x, s, 1))
{
	PrintLn(s);
}
else
{
	PrintLn(x, " - script not found");
}
EndScript

Resource R1
{
	"Test" : "This is a test"
}
EndResource

Script rl
(alias)
RF(alias);
af(alias);
EndScript

Script af
(alias)

String tmp;
String StrError;
U4 ret;

QS_AddScriptFile(StrAssign(tmp, alias, ".qs"), alias, StrError, ret);
if (ret)
{
	PrintLn(StrError);
	return -1;
}
PrintLn(tmp, " added as ", alias);
return 0;

EndScript

Script af_silence
(alias)

String tmp;
String StrError;
U4 ret;

QS_AddScriptFile(StrAssign(tmp, alias, ".qs"), alias, StrError, ret);
return ret;

EndScript


Script AddProjectFile
(fname)
String f_qp, f_qs;
Object o;
U4 ret;
Reference p;
String key;
U0 i;
String StrError;

if (StrReadFile(f_qp, fname))
{
	return -1;
}

JSONParse(o, f_qp, ret);

if (ret)
{
	PrintLn("Failed to JSONParse for ", fname);
	return -1;
}

foreach(p, o["ScriptFile"], i)
{
	QS_AddScriptFile(p[0], p[1], StrError, ret);
	if (ret)
	{
		PrintLn(StrError);
		return -1;
	}
}

return 0;
EndScript

Script RF
(f)

if (QS_RemoveScriptFile(f))
{
	PrintLn(f, " is not found");
}
else
{
	PrintLn(f, " is removed");
}

EndScript

Script SG
(s)
PrintLn(:s);
EndScript